﻿using LBSoft.Business.DAL;
using LBSoft.Business.NewDAL;
using LBSoft.Common.DotNetBean;
using LBSoft.Common.DotNetCode;
using LBSoft.Common.DotNetData;
using LBSoft.Common.DotNetExt;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LBSoft.HplusAdmin.UserControl
{
    public partial class LoadButton : System.Web.UI.UserControl
    {
        public StringBuilder sb_Button = new StringBuilder();
        private readonly CommonDal comidao = new CommonDal();
        private readonly Permission_Dal permissionDal = new Permission_Dal();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int userType = RequestSession.GetSessionUser().UserType;
                string userId = RequestSession.GetSessionUser().UserId;
                string rolesId = permissionDal.GetUserRolesId(userId);

                DataTable dt_Menu = permissionDal.GetMenu(rolesId, userId, userType);

                string pageUrl = RequestHelper.GetScriptName;
                DataTable dt_Button = DataTableHelper.GetNewDataTable(dt_Menu, "ParentId='" + this.GetMenuByNavigateUrl(pageUrl, dt_Menu) + "' AND MenuType = 2");

                if (dt_Button != null && dt_Button.Rows.Count > 0)
                {
                    List<Dictionary<string, string>> tableButtonList = new List<Dictionary<string, string>>();
                    foreach (DataRow dr in dt_Button.Rows)
                    {
                        //添加头部工具栏按钮
                        if (dr["ButtonType"].ToString2() == "1")
                        {
                            string navigateUrl = dr["NavigateUrl"].ToString2();
                            if (!navigateUrl.Contains("()"))
                            {
                                navigateUrl = navigateUrl + "()";
                            }
                            string className = "layui-btn btn-" + navigateUrl.Replace("()", "");
                            this.sb_Button.Append(string.Concat(new string[]
                            {
                                "<button title=\"",
                                dr["MenuName"].ToString2(),
                                "\" onclick=\"",
                                navigateUrl,
                                "\" type=\"button\" class=\"",
                                className,
                                "\" >"
                            }));
                            this.sb_Button.Append(dr["MenuName"].ToString2());
                            this.sb_Button.Append("</button>");
                        }
                        else if (dr["ButtonType"].ToString2() == "2")  //列表上的按钮
                        {
                            Dictionary<string, string> dic = new Dictionary<string, string>();
                            dic["MenuName"] = dr["MenuName"].ToString2();
                            dic["MenuEvent"] = dr["NavigateUrl"].ToString2().Replace("()", "");
                            tableButtonList.Add(dic);
                        }
                    }

                    if (tableButtonList.Count > 0)
                    {
                        this.sb_Button.Append("<span id=\"bst_tableEvent\" style=\"display:none\">");
                        this.sb_Button.Append(JsonConvert.SerializeObject(tableButtonList));
                        this.sb_Button.Append("</span>");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper log = new LogHelper("LoadButton");
                log.WriteExceptionLog(ex);
            }
        }

        public string GetMenuByNavigateUrl(string NavigateUrl, DataTable dt_Menu)
        {
            string result = "";
            try
            {
                DataTable dt = DataTableHelper.GetNewDataTable(dt_Menu, "NavigateUrl='" + NavigateUrl + "'");
                if (dt != null)
                {
                    result = dt.Rows[0]["GUID"].ToString2();
                }
            }
            catch
            {
                result = "";
            }
            return result;
        }
    }
}